/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.console.command;

import org.openislands.oi.console.config.RequestWrapper;
import org.openislands.oi.console.config.annotation.Command;
import org.openislands.oi.console.config.annotation.CommandGroup;
import org.openislands.oi.console.config.annotation.CommandParam;
import org.openislands.oi.pojo.qo.JobQO;
import org.openislands.oi.pojo.qo.JobSubmitQO;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.JobVO;
import org.openislands.oi.util.FileUtils;
import org.openislands.oi.util.ObjectUtils;
import org.openislands.oi.apiserver.http.JobController;

import java.util.List;
import java.util.Map;

@SuppressWarnings("unused")
@CommandGroup("job")
public class JobCommand {

    @Command(value = "submit", desc = "submit job")
    public RequestWrapper<JobController, CommonResponse<JobVO>> submit(
            @CommandParam String jobType,
            @CommandParam(required = false) String userId,
            @CommandParam String name,
            @CommandParam(required = false) String note,
            @CommandParam(desc = "conf path") String conf) {
        return new RequestWrapper<JobController, CommonResponse<JobVO>>() {
            @Override
            public CommonResponse<JobVO> request(JobController target, Map<String, Object> commandParams) {
                commandParams.put("jobType", commandParams.get("jobType").toString().toUpperCase());
                JobSubmitQO jobSubmitQO = ObjectUtils.mapToObject(commandParams, JobSubmitQO.class);
                String conf = FileUtils.readSmallFile(jobSubmitQO.getConf());
                jobSubmitQO.setConf(conf);
                return target.submitJob(jobSubmitQO);
            }

            @Override
            public String resultString(CommonResponse<JobVO> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(value = "query", desc = "query job")
    public RequestWrapper<JobController, CommonResponse<List<JobVO>>> query(
            @CommandParam(required = false) String name,
            @CommandParam(required = false) String jobType,
            @CommandParam(required = false) String jobId,
            @CommandParam(required = false) String state)
    {
        return new RequestWrapper<JobController, CommonResponse<List<JobVO>>>() {
            @Override
            public CommonResponse<List<JobVO>> request(JobController target, Map<String, Object> commandParams) {
                if (commandParams.get("state") != null){
                    commandParams.put("state", commandParams.get("state").toString().toUpperCase());
                }
                JobQO jobQO = ObjectUtils.mapToObject(commandParams, JobQO.class);
                return target.queryJob(jobQO);
            }

            @Override
            public String resultString(CommonResponse<List<JobVO>> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "handle job")
    public RequestWrapper<JobController, CommonResponse<Boolean>> handle(@CommandParam String jobId) {
        return new RequestWrapper<JobController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(JobController target, Map<String, Object> commandParams) {
                return target.handleJob(commandParams.get("jobId").toString());
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "stop job")
    public RequestWrapper<JobController, CommonResponse<Boolean>> stop(@CommandParam String jobId) {
        return new RequestWrapper<JobController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(JobController target, Map<String, Object> commandParams) {
                return target.stopJob(commandParams.get("jobId").toString());
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "scheduling all job")
    public RequestWrapper<JobController, CommonResponse<Boolean>> scheduling() {
        return new RequestWrapper<JobController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(JobController target, Map<String, Object> commandParams) {
                return target.schedulingJobs();
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }
}
